package org.jsrml.sv.mob.controller.base;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.base.command.DeleteRoleCo;
import org.jsrml.core.base.command.SaveResource2RoleCo;
import org.jsrml.core.base.command.SaveRoleCo;
import org.jsrml.core.base.entity.Role;
import org.jsrml.core.base.qo.RoleQO;
import org.jsrml.core.base.service.RoleService;
import org.jsrml.sv.mob.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

	private static Logger logger = LoggerFactory
			.getLogger(RoleController.class);

	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody RoleQO qo) {
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = roleService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Role role = roleService.queryUnique(qo);
				return createSuccessResult(role);
			case 2:
				List<Role> list = roleService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = roleService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
	/**
	 * 删除角色
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JSONResultDTO delete(HttpServletRequest request,
			@RequestBody DeleteRoleCo deleteCommand) {
		try {
			roleService.deleteRole(deleteCommand);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	/**
	 * 新增角色
	 */
	@RequestMapping("/create")
	@ResponseBody
	public JSONResultDTO create(HttpServletRequest request,
			@RequestBody SaveRoleCo createCommand) {
		Role role = null;
		try{
			role = roleService.createRole(createCommand);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(role);
	}

	/**
	 * 修改角色
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public JSONResultDTO modify(HttpServletRequest request,
			@RequestBody SaveRoleCo modifyCommand) {
		Role role = null;
		try{
			role = roleService.modifyRole(modifyCommand);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(role);
	}
	
	/**
	 * 给角色添加菜单资源权限
	 */
	@RequestMapping("/resource/2/role")
	@ResponseBody
	public JSONResultDTO resource2Role(HttpServletRequest request,
			@RequestBody SaveResource2RoleCo command) {
		try{
			roleService.resource2Role(command);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	
}
